package org.springboot.szsrcb.controller;




import java.sql.Date;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springboot.szsrcb.entity.Req_base_info;
import org.springboot.szsrcb.entity.Req_bill;
import org.springboot.szsrcb.entity.Test_bill;
import org.springboot.szsrcb.entity.User;
import org.springboot.szsrcb.srvice.Req_bill_Service;
import org.springboot.szsrcb.srvice.Test_bill_Service;
import org.springboot.szsrcb.srvice.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/test")
public class TestRestController_input {

    private static final Logger logger = LoggerFactory.getLogger(TestRestController_input.class);

	@Autowired
	private Req_bill_Service req_bill_Service;
	
	@Autowired
	private Test_bill_Service test_bill_Service;
	
	@Autowired
	private UserService userService;
	
	
	@RequestMapping(value = "/frank_test", method = { RequestMethod.GET })
	public String frank_test(HttpServletRequest request) {
		
List<Req_bill> req_list=req_bill_Service.get_not_accept_test_list();
		
		String req_titile_list=get_req_title_json(req_list);
		String r_start="["
						+"{type : 'editor',name : 'test_guide',label : '测试指引',position:'label-top', required:'true',inputWidth:'800', inputHeight:'200'}"	
						
						+"]";
		String r_s=r_start;
		return r_s;
				
	}
	
	//@RequestMapping("/req_assign_save")
		@RequestMapping(value = "/test_new_save")
		public boolean test_new_save(HttpServletRequest request, Model model, Test_bill test_bill, HttpSession httpSession) {
			boolean result;
			model.addAttribute("Test_bill", test_bill);
			logger.info("req_id:" + test_bill.getReq_id());
			String login_name=(String) httpSession.getAttribute("login_name");  
			User u =userService.search_a_user(login_name);
			
			result = test_bill_Service.save_test_bill(test_bill,u.getName());

			return result;
		}
		
		
		
		@RequestMapping(value = "/test_new_select_req_no_reqno_save", method = { RequestMethod.POST })
		public int test_new_select_req_no_reqno_save(HttpServletRequest request) {
			boolean result=false;
			String req_title=request.getParameter("req_title");
			String req_bill_no=request.getParameter("req_bill_no");
			logger.info("req_titile:" + req_title);
			logger.info("req_bill_no:" + req_bill_no);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("req_titile", req_title);
			map.put("req_bill_no", req_bill_no);
			Date date = new java.sql.Date(new java.util.Date().getTime());
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");			
			String d_str=sdf.format(date);
			try {
				date=new Date( sdf.parse(d_str).getTime());
				//new Date(req_bill.getAccept_date().getTime());
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			Timestamp timeStamp = new Timestamp(date.getTime());
			map.put("req_bill_create_date", timeStamp);
			result = req_bill_Service.new_req_from_test_bill(map);
			int req_id=0;
			if (result) {
				Req_bill req=req_bill_Service.search_req_bill(map);
				req_id=req.getId();
			} 
			
			//List<Req_bill> r_list=req_bill_Service.req_search_for_test_input(map);
			//int req_id=r_list.get(0).getId();
			//result = test_bill_Service.save_test_bill(test_bill);

			return req_id;
		}
			
		
		
	@RequestMapping(value = "/test_new_2_select_req", method = { RequestMethod.POST })
	public String test_new_2_select_req(HttpServletRequest request) {
		String return_s="";
		String g_req_id= request.getParameter("req_id");
		return_s=trans_2_json_test_new_form(g_req_id);
		
		return return_s;
		
	}
	
	@RequestMapping(value = "/test_new_form_onload", method = { RequestMethod.GET })
	public String req_assign_detail_onload(HttpServletRequest request) {
		String g_req_id= request.getParameter("req_id");
		String return_s="";
		//如果g_req_id为空时
		if (g_req_id.equals("")) {
			return_s=trans_2_json_test_new_form();
		} else {			
			return_s=trans_2_json_test_new_form(g_req_id);
		}
		
		
		return return_s;
		//如果g_req_id不为空
		/*List<Req_bill> r = req_bill_Service.req_search(g_id);
		if(r.size()>0){
			//String s_tmp="req_bill_no:"+r.get(0).getReq_bill_no()+";req_title:"+r.get(0).getReq_title()+";req_type:"+r.get(0).getReq_type()+";req_dept:"+r.get(0).getReq_offer_dept()+";req_complete_date:"+r.get(0).getRequired_completion_date();
			String s_tmp=trans_2_json_assign_req( r.get(0));
			logger.info("req_assign_detail_onload:" + s_tmp);
			return s_tmp;	
		}else{
			return null;	
		}*/
				
	}


	private String trans_2_json_test_new_form(String g_req_id) {
	
		//List<Req_bill> req_list=req_bill_Service.get_All_Req();
		List<Req_bill> req_list=req_bill_Service.get_not_accept_test_list();
		Req_bill req_bill=req_bill_Service.req_bill_search_by_id(g_req_id);
		Req_base_info req_base_info=req_bill_Service.search_req_base_info(g_req_id);
		String req_titile_list=get_req_title_json(req_list,g_req_id);
		
		String tech="";
		//if(req_base_info!=null){
			
			//	tech=req_base_info.getTechnical_director();

		//}
		
		String r_start="["
						+ "{type: 'fieldset', label: '测试单',  list:["
										+"{type:'block',list:["
											+"{type : 'hidden',name : 'req_id',label : '需求ID',labelWidth:'70',position:'label-left',value:'"+g_req_id+"', disabled:'true'}"
										+"]},"
										+"{type:'block',list:["
											+"{type : 'input',name : 'att_group_id',label : 'att_group_id',inputWidth:'150',position:'label-left', hidden:'true',offsetLeft:'25'},"
											+"{type : 'input',name : 'att_id',label : 'att_id',inputWidth:'150',position:'label-left', hidden:'true', offsetLeft:'25'}"										
										+"]},"
										+"{type:'block',list:["
												+"{type : 'select',name : 'req_title',label : '测试项目',inputWidth:'400', required:'true',options:["
														+req_titile_list
												+ "]},"
												+"{type : 'newcolumn'},"
												+"{type : 'button',name : 'select_req_btn',offsetLeft:'25',value : '项目选择'}"
											+"]}," 									
										+"{type:'block',list:["
											+"{type : 'input',name : 'req_bill_no',label : '需求编号',inputWidth:'150',labelWidth:'70',position:'label-left', required:'true',disabled:'true',value:'"+req_bill.getReq_bill_no()+"'},"
											+"{type : 'newcolumn'},"
											+"{type : 'input',name : 'test_technical_director',label : '测试技术负责人',inputWidth:'150',position:'label-left', offsetLeft:'25',offsetLeft:'25',required:'true',value:'"+tech+"'}"										
										+"]},"
										+"{type:'block',list:["
											+"{type : 'select',name : 'test_report_status',label : '自测试报告',inputWidth:'150', required:'true',options:["
													+"{text: '', value: ''},"
													+"{text: '有', value: '有'},"													
													+"{text: '无',value: '无'}"
											+ "]},"
											+"{type : 'newcolumn'},"
											+"{type : 'calendar',name : 'test_complete_date',label : '要求完成日期',position:'label-left', offsetLeft:'25',required:'true'}"
										+"]},"
										+"{type:'block',list:["
											+"{type : 'input',name : 'test_guide',id : 'test_guide',label : '测试指引',inputWidth:'150',position:'label-top', hidden:'true',rows:'3',offsetLeft:'25'},"
											+"{type : 'input',name : 'req_remark',label : '其他说明',inputWidth:'150',position:'label-top', hidden:'true',rows:'3',offsetLeft:'25'}"										
										+"]}"												
										/*+"{type:'block',list:["
											+"{type : 'editor',name : 'test_guide',label : '测试指引',position:'label-top', required:'true',inputWidth:'800', inputHeight:'200'},"
											+"{type : 'editor',name : 'req_remark',label : '其他说明',position:'label-top', required:'true',inputWidth:'800', inputHeight:'200'}"										
										+"]},"
										+"{type:'fieldset',label:'附件',list:["
											+"{type : 'label', name:'att_label', label:'上传文件最大值: 100 Mb'},"
											+"{type:'upload', name:'myFiles', inputWidth:'400', titleText:'拖拽文件到此区域或点击选择文件上传', url:'/attachment/upload', _swfLogs:'enabled',  autoStart:'true', swfPath:'/common/uploader.swf',  swfUrl:'attachment/upload'}"
										+"]},"	
									
										+"{type:'block',list:["
											+"{type : 'button',name : 'test_new_save_btn',value : '送测'}"																	
										+"]}"	*/									
										
									+"]"
							+ "}"
						+"]";
		String r_s=r_start;
		return r_s;
	}


	private String trans_2_json_test_new_form() {
		//List<Req_bill> req_list=req_bill_Service.get_All_Req();
		List<Req_bill> req_list=req_bill_Service.get_not_accept_test_list();
		
		String req_titile_list=get_req_title_json(req_list);
		String r_start="["
						+ "{type: 'fieldset', label: '测试单',  list:["
										+"{type:'block',list:["
											+"{type : 'hidden',name : 'req_id',label : '需求ID',labelWidth:'70',position:'label-left', disabled:'true'}"
										+"]},"
										+"{type:'block',list:["
											+"{type : 'input',name : 'att_group_id',label : 'att_group_id',inputWidth:'150',position:'label-left', hidden:'true',offsetLeft:'25'},"
											+"{type : 'input',name : 'att_id',label : 'att_id',inputWidth:'150',position:'label-left', hidden:'true', offsetLeft:'25'}"										
										+"]},"
										+"{type:'block',list:["
												+"{type : 'select',name : 'req_title',label : '测试项目',inputWidth:'400', required:'true',options:["
														+req_titile_list
												+ "]},"
												+"{type : 'newcolumn'},"
												+"{type : 'button',name : 'select_req_btn',offsetLeft:'25',value : '项目选择'}"
											+"]},"										
										+"{type:'block',list:["
											+"{type : 'input',name : 'req_bill_no',label : '需求编号',inputWidth:'150',labelWidth:'70',position:'label-left', disabled:'true'},"
											+"{type : 'newcolumn'},"
											+"{type : 'input',name : 'technical_director',label : '测试技术负责人',inputWidth:'150',position:'label-left', offsetLeft:'25',disabled:'true',offsetLeft:'25'}"										
										+"]},"
										+"{type:'block',list:["
											+"{type : 'select',name : 'test_report_status',label : '自测试报告',inputWidth:'150', required:'true',options:["
													+"{text: '', value: ''},"
													+"{text: '有', value: '有'},"													
													+"{text: '无',value: '无'}"
											+ "]},"
											+"{type : 'newcolumn'},"
											+"{type : 'calendar',name : 'test_complete_date',label : '要求完成日期',position:'label-left', offsetLeft:'25',required:'true'}"
										+"]},"
										+"{type:'block',list:["
											+"{type : 'input',name : 'test_guide',id : 'test_guide',label : '测试指引',inputWidth:'150',position:'label-top', hidden:'true',rows:'3',offsetLeft:'25'},"
											+"{type : 'input',name : 'req_remark',label : '其他说明',inputWidth:'150',position:'label-top', hidden:'true',rows:'3',offsetLeft:'25'}"										
										+"]}"											
										/*+"{type:'block',list:["
											+"{type : 'editor',name : 'test_guide',label : '测试指引',position:'label-top', required:'true',inputWidth:'800', inputHeight:'200'},"
											+"{type : 'editor',name : 'req_remark',label : '其他说明',position:'label-top', required:'true',inputWidth:'800', inputHeight:'200'}"										
										+"]},"
										+"{type:'fieldset',label:'附件',list:["
											+"{type : 'label', name:'att_label', label:'上传文件最大值: 100 Mb'},"
											+"{type:'upload', name:'myFiles', inputWidth:'400', titleText:'拖拽文件到此区域或点击选择文件上传', url:'/attachment/upload', _swfLogs:'enabled',  autoStart:'true', swfPath:'/common/uploader.swf',  swfUrl:'attachment/upload'}"
										+"]},"	
									
										+"{type:'block',list:["
											+"{type : 'button',name : 'test_new_save_btn',value : '送测'}"																	
										+"]}"											
										*/
									+"]"
							+ "}"
						+"]";
		String r_s=r_start;
		return r_s;
	}


	private String get_req_title_json(List<Req_bill> req_list) {
		//+"{text: '', value: ''},"
		String r="{text: '', value: ''},";
		int i=1;
		for (Req_bill req_bill : req_list) {
			if(i==req_list.size()){
				r=r+"{text: '"+req_bill.getReq_title()+"', value: '"+req_bill.getId()+"'}";
			}else{
				r=r+"{text: '"+req_bill.getReq_title()+"', value: '"+req_bill.getId()+"'},";
			}
			i=i+1;
		}
		return r;
	}
	
	private String get_req_title_json(List<Req_bill> req_list,String req_id) {
		//+"{text: '', value: ''},"
		String r="{text: '', value: ''},";
		int i=1;
		String select_s="";
		for (Req_bill req_bill : req_list) {
			if (req_bill.getId()==Integer.parseInt(req_id)) {
				select_s=" selected:'true',";
			} else{
				 select_s="";
			}
			if(i==req_list.size()){
				r=r+"{text: '"+req_bill.getReq_title()+"', "+select_s+" value: '"+req_bill.getId()+"'}";
			}else{
				r=r+"{text: '"+req_bill.getReq_title()+"', "+select_s+" value: '"+req_bill.getId()+"'},";
			}
			i=i+1;
		}
		return r;
	}
	
	
	@RequestMapping(value = "/req_search_for_test_input", method = { RequestMethod.GET })
	public String req_search_for_test_input(HttpServletRequest request) {
		// List<Req_bill> result=false;
		String g_p = request.getParameter("params");
		String[] s_array = g_p.split(",",4);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("req_titile", s_array[0]);
		map.put("req_bill_no", s_array[1]);
		map.put("offer_dept", s_array[2]);
		map.put("deffect_no", s_array[3]);
	/*	if (s_array.length > 0) {
			map.put("req_titile", s_array[0]);
			if (s_array.length > 1) {
				map.put("req_bill_no", s_array[1]);
				if (s_array.length > 2) {
					map.put("offer_dept", s_array[2]);
					if (s_array.length > 3) {
						map.put("deffect_no", s_array[3]);
					}
				}
			}
		}*/
		List<Req_bill> r = req_bill_Service.req_search_for_test_input(map);
		
		if(r!=null){
		
			String s=trans_2_json(r);
			//String s="{rows:[ { id:1,data:[ '0','5','req_no','req_typeJohn Grisham','12.99','1','05/01/1998'] },{ id:2,data:[ '1','2','req_no','req_typeJohn Grisham','12.99','1','05/01/1998'] }]}";
			//String s="{rows:[ { id:1,data:[ '0','5','req_no','req_typeJohn Grisham','12.99','1','05/01/1998'] }]}";
			return s;
		}else{
			return null;
		}		
	}

	private String trans_2_json(List<Req_bill> obj) {
		String r_start="{rows:[";		
		String r_end="]}";
		String r_content="";
		
		for (int i = 0; i < obj.size(); i++) {
			Req_bill element = obj.get(i);
			String tmp_start="{ id:"+(i+1)+",data:[";
			String tmp_end="]}";
			if((i+1)<obj.size()){
				tmp_end=tmp_end+",";
			}
			
			String tmp_content="'0',";
			//String tmp_content="'2',";
			tmp_content=tmp_content+"'"+element.getId()+"',";
			tmp_content=tmp_content+"'"+element.getReq_bill_no()+"',";
			tmp_content=tmp_content+"'"+element.getReq_type()+"',";
			tmp_content=tmp_content+"'"+element.getReq_title()+"',";
			tmp_content=tmp_content+"'"+element.getReq_offer_dept()+"',";
			tmp_content=tmp_content+"'"+element.getDeffect_no()+"'";
			//tmp_content=tmp_content+"'"+element.getReq_status()+"'";
			r_content=r_content+tmp_start+tmp_content+tmp_end;
		}
		String r_s=r_start+r_content+r_end;
		logger.info("trans_2_json :" + r_s);
		return r_s;
	}
	
}
